﻿#include "CMutexDefault.h"



namespace lampe{
namespace thread{

CMutexDefault::CMutexDefault(){
	locked=false;
#ifdef _IRR_COMPILE_WITH_X11_DEVICE_
	pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_NORMAL);
    pthread_mutex_init(&m_mutex, &attr);
    pthread_mutexattr_destroy(&attr);
#endif
#ifdef _IRR_WINDOWS_
	InitializeCriticalSection(&m_CriticalSection);
#endif
}

CMutexDefault::~CMutexDefault(){
#ifdef _IRR_COMPILE_WITH_X11_DEVICE_
	pthread_mutex_destroy(&m_mutex);
#endif
#ifdef _IRR_WINDOWS_
	DeleteCriticalSection(&m_CriticalSection);
#endif
}

void CMutexDefault::lock(){
#ifdef _IRR_WINDOWS_
	EnterCriticalSection(&m_CriticalSection);
#endif
#ifdef _IRR_COMPILE_WITH_X11_DEVICE_
	pthread_mutex_lock(&m_mutex);
#endif
	locked=true;
}
void CMutexDefault::unlock(){
	locked=false;
#ifdef _IRR_WINDOWS_
	LeaveCriticalSection(&m_CriticalSection);
#endif
#ifdef _IRR_COMPILE_WITH_X11_DEVICE_
	pthread_mutex_unlock(&m_mutex);
#endif

}

bool CMutexDefault::isLocked(){
	return locked;
}

extern IMutex* createDefaultMutex(){
	return new CMutexDefault();
}

}//namespace thread
}//namespace irr
